package android.vstore.fragment;

import java.util.ArrayList;

import com.appvn2014.appstorevn.R;

import DAO.ProductDAO;
import DTO.CollectionDTO;
import DTO.ProductDTO;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.vstore.activities.BaseActivity;
import android.vstore.activities.MainActivity;
import android.vstore.activities.SceneGroupActivity;
import android.widget.Button;
import android.widget.ImageView;
import anroid.vstore.provider.Constant;
import anroid.vstore.provider.Navigator;

@SuppressLint("ValidFragment")
public class SceneHomeFragment extends BaseFragment implements OnClickListener {



	private View rootView;
	private Button btn_app,btn_game,btn_video,btn_comic,btn_news,btn_ebook;
	Boolean isReload = false;
	public BaseActivity activity;


	public static SceneHomeFragment newinstance(BaseActivity activity)
	{
		SceneHomeFragment home = new SceneHomeFragment();
		home.activity = activity;
		home.name = "    HOME    ";
		return home;
	}
	public SceneHomeFragment()
	{
		super();
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// Check which request we're responding to
		// Make sure the request was successful
		if (resultCode == Constant.TYPE_ALL) {
			//			mViewPager.setCurrentItem(1);
		}
//		else if(requestCode == Constant.APP_INSTALLATION)
//		{
//			for(int i =0; i < lstFrags.size();i++)
//			{
//				lstFrags.get(i).Invalidate();
//			}
//		}

	}

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState); 
		isReload = true;
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.fragment_home, container, false);
		InitGUI();
		startThreads();
		isReload = false;
		return rootView;
	}
	@Override
	public void InitGUI()
	{
		super.InitGUI();
		btn_app = ((Button)rootView.findViewById(R.id.btn_home_app));
		btn_game = ((Button)rootView.findViewById(R.id.btn_home_game));
		btn_video = ((Button)rootView.findViewById(R.id.btn_home_video));
		btn_ebook = ((Button)rootView.findViewById(R.id.btn_home_ebook));
		btn_comic = ((Button)rootView.findViewById(R.id.btn_home_comic));
		btn_news = ((Button)rootView.findViewById(R.id.btn_home_news));

		btn_app.setOnClickListener(this);
		btn_game.setOnClickListener(this);
		btn_video.setOnClickListener(this);
		btn_ebook.setOnClickListener(this);
		btn_comic.setOnClickListener(this);
		btn_news.setOnClickListener(this);


	}
	@Override
	public void startThreads()
	{
		if(!isReload|| activity == null)
			return;
		//		super.startThreads();

		(new Loader()).execute();

	}

	ArrayList<PageHorizalGalleryFragment> lstFrags = new ArrayList<PageHorizalGalleryFragment>();
	private void CreateHorizalGallery(int id,String collectionId,String name) {
		PageHorizalGalleryFragment hlv_1 = PageHorizalGalleryFragment.instance(activity, id, collectionId,name);
		lstFrags.add(hlv_1);
		if(hlv_1 != null)
			getActivity().getSupportFragmentManager().beginTransaction()
			.add(R.id.ll_home_body, hlv_1).commit();
	}
	@Override
	public void onClick(View v) {
		Intent intent = new Intent(activity,SceneGroupActivity.class);


		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_home_app:
			intent.putExtra("type", Constant.TYPE_APPLICATION);
			activity.startActivityForResult(intent, 0);
			break;
		case R.id.btn_home_game:
			intent.putExtra("type", Constant.TYPE_GAME);
			activity.startActivityForResult(intent, 0);
			break;
		case R.id.btn_home_video:
			//			Navigator.toItemList(activity, 0);
			Navigator.toVideosList(activity);
			//			activity.ShowUpdatingFunction();
			break;
		case R.id.btn_home_comic:
			activity.ShowUpdatingFunction();
			break;
		case R.id.btn_home_ebook:
			activity.ShowUpdatingFunction();
			break;
		case R.id.btn_home_news:
			activity.ShowUpdatingFunction();
			break;
		default:
			break;
		}

	}
	public class Loader extends AsyncTask<String, Void, ArrayList<CollectionDTO>>{
		@Override
		protected ArrayList<CollectionDTO> doInBackground(String... params) {
			ArrayList<CollectionDTO> lstCollection = ProductDAO.GetCollections();
			return lstCollection;
		}      

		@Override
		protected void onPostExecute(ArrayList<CollectionDTO> lstCollection) {
			int size = lstCollection.size();
			for(int i =0 ; i < size; i++)
			{
				CreateHorizalGallery(Constant.HORIZALLIST_COLLECTION,((Integer)lstCollection.get(i).id).toString(),lstCollection.get(i).name);
			}

		}

		@Override
		protected void onPreExecute() {
		}

		@Override
		protected void onProgressUpdate(Void... values) {
		}
	}

}
